package com.hutao.gateway.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.alibaba.nacos.common.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hutao.gateway.entity.AuthPermission;
import com.hutao.gateway.entity.AuthRole;
import com.hutao.gateway.redis.RedisUtil;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限验证接口扩展
 *
 * @author: ChickenWing
 * @date: 2023/10/28
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";

    private String authRolePrefix = "auth.role";

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String authKey = redisUtil.buildKey(authPermissionPrefix, loginId.toString());
        String authValue = redisUtil.get(authKey);
        if(StringUtils.isBlank(authValue)){
            return Collections.emptyList();
        }
        List<String> authList;
        List<AuthPermission> permissionList = new Gson().fromJson(authValue,
                new TypeToken<List<AuthPermission>>() {
        }.getType());
        authList = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }


    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String authKey = redisUtil.buildKey(authRolePrefix, loginId.toString());
        String authValue = redisUtil.get(authKey);
        if(StringUtils.isBlank(authValue)){
            return Collections.emptyList();
        }
        List<String> authList;
        List<AuthRole> roleList = new Gson().fromJson(authValue,
                new TypeToken<List<AuthRole>>() {
                }.getType());
        authList = roleList.stream().map(AuthRole::getRoleKey).collect(Collectors.toList());
        return authList;
    }
}
